package com.guli.edu.service.impl;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.CannedAccessControlList;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.common.handler.EduException;
import com.guli.common.result.ResultCodeEnum;
import com.guli.edu.service.EduTeacherService;
import com.guli.edu.entity.EduTeacher;
import com.guli.edu.mapper.EduTeacherMapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.edu.vo.QueryTeacher;
import org.joda.time.DateTime;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 讲师 服务实现类
 * </p>
 *
 * @author Daniel
 * @since 2019-03-19
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {

    @Override
    public void pageList(Page<EduTeacher> pageParam, QueryTeacher queryTeacher) {
        if(queryTeacher==null){
            baseMapper.selectPage(pageParam,null);
        }
        String name = queryTeacher.getName();
        Integer level = queryTeacher.getLevel();
        String begin = queryTeacher.getBegin();
        String end = queryTeacher.getEnd();
        QueryWrapper wrapper = new QueryWrapper();
        //判断条件值是否为空
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }
        if(!StringUtils.isEmpty(level)){
            wrapper.eq("level",level);
        }
        if (!StringUtils.isEmpty(begin)){
            wrapper.ge("gmt_create",begin);
        }
        if (!StringUtils.isEmpty(begin)){
            wrapper.le("gmt_create",end);
        }
        baseMapper.selectPage(pageParam,wrapper);
    }

    @Override
    public boolean deleteById(String id) {
        Integer rows = baseMapper.deleteById(id);
        return null != rows && rows>0;
    }

    @Override
    public Map<String, Object> pageTeacherList(Page<EduTeacher> pageTeacher) {
       QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
       queryWrapper.orderByAsc("sort");
       baseMapper.selectPage(pageTeacher,queryWrapper);
        List<EduTeacher> records = pageTeacher.getRecords();
        long current = pageTeacher.getCurrent();
        long total = pageTeacher.getTotal();
        long size = pageTeacher.getSize();
        long pages = pageTeacher.getPages();
        boolean hasNext = pageTeacher.hasNext();
        boolean hasPrevious = pageTeacher.hasPrevious();

        Map<String ,Object> map = new HashMap<>();
        map.put("items",records);
        map.put("current",current);
        map.put("total",total);
        map.put("pages",pages);
        map.put("size",size);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);
        return map  ;
    }

}
